package com.shipwe.loans.ui.presenter

import com.shipwe.loans.configs.ShipweConfigs
import com.shipwe.loans.http.ApiService
import com.shipwe.loans.http.ApiService.addCommonParams
import com.shipwe.loans.http.IHttpCallback
import com.shipwe.loans.pro.ShipwePresenter
import com.shipwe.loans.ui.view.IOrderPayPinStatusView
import com.shipwe.loans.utils.shared.ShipweSharedHelper
import com.shipwe.loans.utils.shared.ShipweSharedKey
import org.json.JSONObject

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/27 13:45
 * @Annotation：支付状态查询 Presenter 实现
 */
class OrderPayPinStatusPresenter(private val iView: IOrderPayPinStatusView) : ShipwePresenter(iView) {

    //三方订单id
    private var thirdOrderId = ""
    fun saveOrderPayPinThirdOrderId(orderId: String){
        thirdOrderId = orderId
    }

    /**
     * 获取支付状态
     */
    fun loadOrderPaymentPinStatusAction(isStartCountdown: Boolean = true){
        iView.showShipweLoading()
        val params = JSONObject().addCommonParams().apply {
            put("path",ApiService.API_PATH.SHIPWE_API_LOAN_REPAYMENT_STATUS_PATH)
            put("product",iView.getOrderPayProductName())
            put("token",iView.getOrderPayProductToken())
            put("thirdOrderId",thirdOrderId)//三方订单id
        }
        val httpUrl = ApiService.getHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                iView.dismissShipweLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null) {
                    obj.put("isStartCountdown",isStartCountdown)
                    if (obj.has("transactionId")){
                        thirdOrderId = obj.optString("transactionId")
                    }

                    // PROCESSING /SUCCEEDED /FAILED
                    val status = obj.optString("status")
                    when(status){
                        "FAILED" -> {
                            iView.showOrderPaymentFailed(obj)
                        }
                        "SUCCEEDED" -> {
                            iView.showOrderPaymentSuccess(obj)
                        }
                        else -> {
                            iView.showOrderPaymentProcess(obj)
                        }
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
                iView.dismissShipweLoading()
            }
        })
    }


    /**
     * 订单还款
     */
    fun loadOrderPayLinkAction(){
        iView.showShipweLoading()
        val params = JSONObject().addCommonParams().apply {
            put("product",iView.getOrderPayProductName())
            put("token",iView.getOrderPayProductToken())
            put("mobile", ShipweSharedHelper.getShipweSharedString(ShipweSharedKey.SHIPWE_MOBILE))
            put("path",ApiService.API_PATH.SHIPWE_API_LOAN_REPAYMENT_URL_PATH)
        }
        if (ShipweConfigs.isUgx() || ShipweConfigs.isKsh()){//肯尼亚、乌干达 需要使用 getOrderRepayH5 获取链接
            params.put("path",ApiService.API_PATH.SHIPWE_API_LOAN_REPAYMENT_KSH_UGX_URL_PATH)
        }
        //是否使用积分抵扣
        if (iView.getOrderPayPointsCount().isNotEmpty()){
            params.put("annulType","score")//不减免 normal  积分减免 score  优惠券减免 coupon
            params.put("score",iView.getOrderPayPointsCount())
        }

        val httpUrl = ApiService.getHttpUrl()
        doHttpPostAction(httpUrl,params,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                iView.dismissShipweLoading()
                val obj = data.optJSONObject("obj")
                if (obj != null && obj.has("thirdOrderId")){
                    thirdOrderId = obj.optString("thirdOrderId")
                }
                loadOrderPaymentPinStatusAction()
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }

}